const api = require('./api')

  // 还原回收站
const recovery=(that,e)=>{
    let label_id = e.currentTarget.id
    let set_status =2
    api.set_label_status(that,label_id,set_status)
  }

  // 显/隐回收站
const show_deleted=(that)=>{
  if(that.data.show_deleted){
    that.setData({
      show_deleted:false
    })
  }else{
    api.load_deleted_jgLabels(that, that.jg_id)
  }
}



  // 设置标签选项
const set_option=(that,e)=>{
    let index = e.currentTarget.id
    that.setData({
      selected_index:index
    })
    var label_id = that.data.jg_label_list[index].label_id
    let label_name = that.data.jg_label_list[index].label_name
    wx.showActionSheet({
      itemList: ['修改','删除'],
      success: res => {
        if(res.tapIndex==0){
          wx.showModal({
            content: label_name,
            editable: true,
            showCancel: true,
            title: '修改组名',
            success: res => {
              if(res.confirm){
                let set_name = res.content
                api.update_label_name(that,label_id,set_name)
              }
            },
          })
        }else if(res.tapIndex==1){
          wx.showModal({
            content: '确定要删除【'+label_name+'】吗？',
            showCancel: true,
            title: '警告',
            success: res => {
              if(res.confirm){
                api.set_label_status(that,label_id,3)
              }
            },
          })
        }
      },
    })
  }



  // 组名显示/隐藏
const show_or_hidden=(that,e)=>{
    let label_id = e.currentTarget.id
    let value = e.detail.value
    let set_status = value?1:2
    api.set_label_status(that,label_id,set_status)
  }


  // 永久删除标签  //【9】-彻底删除
  const delete_forever=(that, e)=>{
    let index = e.currentTarget.id

    that.setData({
      selected_index:index
    })

    let label_name_num = that.data.deleted_jg_label_list[index].label_name_num
    let num = label_name_num.split('（')[1].slice(0,-1)
    if(num > 0){
      wx.showModal({
        content: '该分组/批次下，还有学员，不建议彻底删除。\n若特殊情况，可联系客服。',
        showCancel: false,
        title: '提醒',
      })
      return
    }

    let label_name = that.data.deleted_jg_label_list[index].label_name
    let label_id = that.data.deleted_jg_label_list[index].label_id
    wx.showActionSheet({
      itemList: ['彻底删除'],
      success: res => {
        if(res.tapIndex==0){
          wx.showModal({
            content: '确定要彻底删除【'+ label_name +'】吗？',
            showCancel: true,
            title: '提醒',
            success: res => {
              if(res.confirm){
                api.set_label_status(that,label_id, 9)  //9-彻底删除
              }
            },
          })
        }
      },
    })
  }

  module.exports={
    recovery,
    show_deleted,
    set_option,
    show_or_hidden,
    delete_forever,
  }